﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class VacationDetailEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                    ReqVacationSub reqVacationSub = ctx.ReqVacationSubs.Single(r => r.Id == int.Parse(id));
                    PreStartTime.Value = reqVacationSub.PreStartTime.ToString("yyyy-MM-dd HH:mm:ss");
                    PreEndTime.Value = reqVacationSub.PreEndTime.ToString("yyyy-MM-dd HH:mm:ss");
                    Remarks.Value = reqVacationSub.Remarks;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            ReqVacationSub reqVacationSub;
            string id = Request.QueryString["id"];
            if (string.IsNullOrEmpty(id))
            {
                reqVacationSub = new ReqVacationSub();
                reqVacationSub.FormNo = Request.QueryString["formNo"];
                ctx.ReqVacationSubs.InsertOnSubmit(reqVacationSub);
            }
            else
            {
                reqVacationSub = ctx.ReqVacationSubs.Single(r => r.Id == int.Parse(id));
            }
            reqVacationSub.PreStartTime = DateTime.Parse(PreStartTime.Value);
            reqVacationSub.PreEndTime = DateTime.Parse(PreEndTime.Value);
            reqVacationSub.Remarks = Remarks.Value;
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='Succeed';window.close();</script>");
        }
    }
}